﻿package mortal.game.view.mainUI.avatar
{
    import Message.DB.Tables.*;
    import Message.Game.*;
    import Message.Public.*;
    import com.mui.controls.*;
    import com.mui.utils.*;
    import extend.language.*;
    import fl.events.*;
    import flash.events.*;
    import mortal.common.*;
    import mortal.common.display.*;
    import mortal.component.gconst.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.tableConfig.*;
    import mortal.game.scene3D.layer3D.utils.*;
    import mortal.game.scene3D.player.entity.*;
    import mortal.game.scene3D.player.info.*;
    import mortal.game.utils.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.cd.*;
    import mortal.game.view.common.cd.effect.*;
    import mortal.game.view.drugBack.*;
    import mortal.game.view.mainUI.roleAvatar.*;
    import mortal.game.view.systemSetting.*;
    import mortal.mvc.core.*;

    public class PetAvatar extends EntityAvatar
    {
        protected var _lifeSlider:GSlider;
        protected var _cdDataLife:ICDData;
        protected var _freezingLifeEffect:CDDrugFreezingEffect;
        protected var _liftPackBtn:GLoadedButton;

        public function PetAvatar()
        {
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            this.addLifeSlider();
            this.addLifePackBtn();
            this.updateCDEffect();
            this.configEventListener(MouseEvent.CLICK, this.setTarget);
            return;
        }// end function

        override protected function addLifeBar() : void
        {
            _shengmingBar = UICompomentPool.getUICompoment(BaseProgressBar);
            _shengmingBar.createDisposedChildren();
            _shengmingBar.setProgress(ImagesConst.AvatarMonsterLife, false, 2, 0, 119, 13);
            _shengmingBar.setBg(BitmapDataConst.AlphaBMD, true, 119, 13);
            _shengmingBar.setLabel(BaseProgressBar.ProgressBarTextOver, -35, -5);
            _shengmingBar.isCacheBmp = true;
            _shengmingBar.mouseChildren = false;
            _shengmingBar.mouseEnabled = true;
            _shengmingBar.x = 52;
            _shengmingBar.y = 24;
            _shengmingBar.toolTipData = this.getLiftUseDrugToolTip;
            _shengmingBar.configEventListener(MouseEvent.CLICK, this.changeLifeSliderPos);
            this.addChild(_shengmingBar);
            return;
        }// end function

        override protected function addAvatar() : void
        {
            _avatar = new GImageBitmap();
            _avatar.x = 7;
            _avatar.y = 6;
            this.addChild(_avatar);
            this.pushUIToDisposeVec(UIFactory.gBitmap(ImagesConst.AvatarCircle, 5, 5, this));
            return;
        }// end function

        private function changeLifeSliderPos(event:MouseEvent) : void
        {
            this._lifeSlider.value = _shengmingBar.mouseX - 5;
            this.onLifeSetChangeHandler();
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            super.disposeImpl(param1);
            this._lifeSlider.dispose(param1);
            this._lifeSlider = null;
            this._liftPackBtn.dispose(param1);
            this._liftPackBtn = null;
            return;
        }// end function

        public function updateCDEffect(param1:int = 1) : void
        {
            this.unRegisterEffects();
            this._cdDataLife = Cache.instance.cd.getCDData(DrugPackUtil.getCdKeyByType(EDrug._EDrugPetLifeBag), CDDataType.itemData);
            this.registerEffects();
            return;
        }// end function

        protected function unRegisterEffects() : void
        {
            if (this._cdDataLife)
            {
                this.unRegisterLifeFreezing();
                this._cdDataLife.removeFinishCallback(this.cdLifeFinished);
            }
            return;
        }// end function

        protected function unRegisterLifeFreezing() : void
        {
            if (this._freezingLifeEffect && this._freezingLifeEffect.registed)
            {
                this._freezingLifeEffect.reset();
                this._cdDataLife.removeEffect(this._freezingLifeEffect);
                DisplayUtil.removeMe(this._freezingLifeEffect);
            }
            return;
        }// end function

        protected function registerEffects() : void
        {
            if (this._cdDataLife)
            {
                this.registerLifeFreezingEffect();
                this._cdDataLife.addFinishCallback(this.cdLifeFinished);
            }
            return;
        }// end function

        protected function cdLifeFinished() : void
        {
            return;
        }// end function

        protected function registerLifeFreezingEffect() : void
        {
            if (this._cdDataLife == null)
            {
                return;
            }
            if (this._freezingLifeEffect == null)
            {
                this._freezingLifeEffect = new CDDrugFreezingEffect();
                this._freezingLifeEffect.x = this._liftPackBtn.x + 10;
                this._freezingLifeEffect.y = this._liftPackBtn.y + 10;
                this._freezingLifeEffect.setSize(this._liftPackBtn.width - 3, this._liftPackBtn.height - 4);
            }
            if (this._freezingLifeEffect.parent == null)
            {
                this.addChild(this._freezingLifeEffect);
            }
            if (!this._freezingLifeEffect.registed)
            {
                this._cdDataLife.addEffect(this._freezingLifeEffect);
            }
            return;
        }// end function

        override protected function addBuff() : void
        {
            _buffPanel = UICompomentPool.getUICompoment(BuffPanel);
            _buffPanel.createDisposedChildren();
            _buffPanel.x = 65;
            _buffPanel.y = 38;
            this.addChild(_buffPanel);
            return;
        }// end function

        protected function addLifePackBtn() : void
        {
            this._liftPackBtn = UIFactory.gLoadedButton(ImagesConst.PackLiftDrug_upSkin, 177, 18, 19, 19, this, false);
            this._liftPackBtn.toolTipData = this.getLiftDrugToolTip;
            this._liftPackBtn.configEventListener(MouseEvent.CLICK, this.showDrugWin);
            return;
        }// end function

        protected function addLifeSlider() : void
        {
            this._lifeSlider = new GSlider();
            this._lifeSlider.x = 54;
            this._lifeSlider.y = 20;
            this._lifeSlider.styleName = "SliderBtn";
            this._lifeSlider.width = 114;
            this._lifeSlider.height = 13;
            this._lifeSlider.buttonMode = true;
            this._lifeSlider.maximum = 114;
            this._lifeSlider.minimum = 0;
            this._lifeSlider.snapInterval = 1;
            this._lifeSlider.liveDragging = true;
            this._lifeSlider.mouseEnabled = true;
            this._lifeSlider.toolTipData = this.getLiftUseDrugToolTip;
            addChild(this._lifeSlider);
            this._lifeSlider.configEventListener(SliderEvent.THUMB_RELEASE, this.onLifeSetChangeHandler);
            return;
        }// end function

        private function updatePet(param1:Object) : void
        {
            var _loc_2:* = Cache.instance.pet.getSpetByUid(param1 as String);
            this.onPetLifeUpdate(_loc_2);
            return;
        }// end function

        public function onPetLifeUpdate(param1:Object) : void
        {
            var _loc_2:* = param1 as SPet;
            if (_loc_2)
            {
                _shengmingBar.setValue(_loc_2.publicPet.life, PetUtil.getMaxLife(_loc_2));
            }
            return;
        }// end function

        public function specialUpdateLevel(param1:int) : void
        {
            _level.text = param1.toString();
            NetDispatcher.addCmdListener(ServerCommand.PetAttributeUpdate, this.updatePet);
            return;
        }// end function

        public function specialUpdateAvatar(param1:int) : void
        {
            var _loc_2:* = PetConfig.instance.getInfoByCode(param1);
            _avatar.imgUrl = _loc_2.avatar + ".png";
            return;
        }// end function

        override public function updateLife(event:Event = null) : void
        {
            super.updateLife(event);
            return;
        }// end function

        private function setTarget(event:MouseEvent) : void
        {
            var _loc_3:IEntity = null;
            var _loc_2:* = ThingUtil.entityUtil.getSelfPetEntity();
            if (_entityInfo && _loc_2)
            {
                _loc_3 = ThingUtil.entityUtil.getEntity(_loc_2.entityInfo.entityInfo.entityId);
                ThingUtil.selectEntity = _loc_3;
            }
            return;
        }// end function

        override public function updateEntity(param1:EntityInfo) : void
        {
            super.updateEntity(param1);
            return;
        }// end function

        override public function updateAvatar() : void
        {
            if (_entityInfo)
            {
                _avatar.bitmapData = null;
                _avatar.imgUrl = PetConfig.instance.getInfoByCode(_entityInfo.entityInfo.code).avatar + ".png";
            }
            return;
        }// end function

        override public function updateLevel(event:Event = null) : void
        {
            super.updateLevel(event);
            return;
        }// end function

        override protected function addEntityEvent() : void
        {
            super.addEntityEvent();
            if (_entityInfo)
            {
                NetDispatcher.removeCmdListener(ServerCommand.PetAttributeUpdate, this.updatePet);
                NetDispatcher.removeCmdListener(ServerCommand.PetLifeUpdate, this.onPetLifeUpdate);
            }
            return;
        }// end function

        override protected function removeEntityEvent(event:EntityInfoEvent = null) : void
        {
            var _loc_2:PetCache = null;
            super.removeEntityEvent(event);
            if (_entityInfo)
            {
                NetDispatcher.addCmdListener(ServerCommand.PetLifeUpdate, this.onPetLifeUpdate);
                NetDispatcher.removeCmdListener(ServerCommand.PetAttributeUpdate, this.updatePet);
                _buffPanel.clean();
                _loc_2 = Cache.instance.pet;
                this.onPetLifeUpdate(_loc_2.getSpetByUid(_loc_2.lastFightUid));
            }
            return;
        }// end function

        public function refreshDrugNum() : void
        {
            this._liftPackBtn.filters = Cache.instance.DrugPack.petLiftData.leftNum != 0 ? ([]) : ([FilterConst.colorFilter2]);
            return;
        }// end function

        public function getLiftDrugToolTip() : String
        {
            var _loc_1:* = Language.getString(35360);
            return Language.getStringByParam(30153, _loc_1, _loc_1, _loc_1, DrugPackUtil.getRecoverValueByType(EDrug._EDrugPetLifeBag), _loc_1, Cache.instance.DrugPack.petLiftData.leftNum);
        }// end function

        public function getLiftUseDrugToolTip() : String
        {
            var _loc_1:* = Language.getString(35360);
            return "<font color=\'#1ccc31\'>" + _shengmingBar.value + "/" + _shengmingBar.totalValue + "</font>\n" + Language.getStringByParam(30154, _loc_1, Math.round(this._lifeSlider.value / this._lifeSlider.maximum * 100), _loc_1);
        }// end function

        private function showDrugWin(event:MouseEvent) : void
        {
            var _loc_2:int = 2;
            Dispatcher.dispatchEvent(new DataEvent(EventName.OpenDrugWin, _loc_2));
            return;
        }// end function

        private function onLifeSetChangeHandler(event:SliderEvent = null) : void
        {
            ClientSetting.local.setDrugPack(Math.round(this._lifeSlider.value / this._lifeSlider.maximum * 100), EDrug._EDrugPetLifeBag);
            Cache.instance.DrugPack.petLiftData.autoNum = this._lifeSlider.value / this._lifeSlider.maximum;
            Dispatcher.dispatchEvent(new DataEvent(EventName.PetLifeDrugUse, null));
            return;
        }// end function

        public function setClientValue() : void
        {
            var _loc_1:* = ClientSetting.local.drugPackSetting[EDrug._EDrugPetLifeBag];
            this._lifeSlider.value = ClientSetting.local.drugPackSetting[EDrug._EDrugPetLifeBag] * this._lifeSlider.maximum / 100;
            return;
        }// end function

    }
}
